<template>
    <div class="action-bar">
        <el-button
          type="primary"
          @click="showGroupAdd"
        >
          添加小组
        </el-button>

        <el-button
          type="danger"
          @click="changeGroupType"
        >
          {{parseInt(groupType, 10) === 0?'我加入的小组':'我创建的小组'}}
        </el-button>
      </div>
    <Table
        ref="tableInfoRef"
        :columns="columns"
        :fetch="loadDataList"
        :dataSource="tableData"
        :options="tableOptions"
        :extHeight="tableOptions.extHeight"
        :selected="selectedHandler"
        @rowSelected="rowSelected"
      >
        <template #slotCover="{ index, row }">
          <Cover
            v-if="row.coverType == 0"
            title="无封面"
            bgColor="#ddd"
            :width="100"
            :height="100"
          ></Cover>
          <Cover
            v-if="row.coverType == 1"
            :width="200"
            :height="100"
            :cover="row.coverPath"
          ></Cover>
          <Cover
            v-if="row.coverType == 2"
            :width="100"
            :height="100"
            :cover="row.coverPath"
          ></Cover>
        </template>

        <template #slotTitle="{ index, row }">
          <span class="a-link" >{{
            row.title
          }}</span>
        </template>

        <template #slotStatus="{ index, row }">
          <Badge showType="orange" text="待发布" v-if="row.status == 0"></Badge>
          <Badge showType="green" text="已发布" v-if="row.status == 1"></Badge>
        </template>

        <template #slotOperation="{ index, row }">
          <div class="row-op-panel" >
            <a
              class="a-link"
              href="javascript:void(0)"
              @click="showGroupEdit(row)"
              v-if="row.creatorId===userInfo.userId"
              >修改</a
            >
            <a
              class="a-link"
              href="javascript:void(0)"
              @click="delInfo(row)"
              v-if="row.creatorId===userInfo.userId"
              >删除</a
            >
            <a
              class="a-link"
              href="javascript:void(0)"
              @click="showGropChat(row)"
              >进入讨论</a
            >
          </div>
        </template>
      </Table>
      <GroupAddEdit ref="groupAddRef" @reload="loadDataList"></GroupAddEdit>
      <GroupUpdateEdit ref="groupUpdateRef" @reload="loadDataList"></GroupUpdateEdit>
      <UserGroupChat ref="userGroupChatRef"></UserGroupChat>
  </template>

<script setup>
import { ref, reactive, getCurrentInstance, nextTick } from "vue"
const { proxy } = getCurrentInstance();
import GroupAddEdit from "./group/GroupAddEdit.vue";
import GroupUpdateEdit from "./group/GroupUpdateEdit.vue";
import UserGroupChat from "./group/UserGroupChat.vue";

const api={
  loadDataList:"/group/loadDataList",
  delInfo: "/group/delete",
}
const userInfo=ref(proxy.VueCookies.get("userInfo"))

const tableData = ref({});
const tableOptions = ref({
  selectType: "checkbox",
  extHeight: "105",
});

const columns = [
  {
    label: "群组id",
    prop: "groupId",
    width: 80,
  },

  {
    label: "小组名称",
    prop: "name",
  },
  {
    label: "小组描述",
    prop: "description",
    width: 150,
  },
  {
    label: "创建时间",
    prop: "createTime",
    width: 180,
  },
  {
    label: "更新时间",
    prop: "updateTime",
    width: 180,
  },
  {
    label: "操作",
    width: 130,
    scopedSlots: "slotOperation",
  },
];

const groupAddRef=ref({});
const showGroupAdd=()=>{
    groupAddRef.value.showEdit()
}

const groupUpdateRef=ref({});
const showGroupEdit=(data)=>{
    groupUpdateRef.value.showEdit(data);
}


const searchForm=ref({});
const tableInfoRef = ref();

const groupType=ref(0);
const changeGroupType=()=>{
   // 使用赋值操作来更新 groupType 的值
    groupType.value = groupType.value === 0 ? 1 : 0;
    loadDataList();
}
const loadDataList = async () => {
    let params = {
    pageNo: tableData.value.pageNo,
    pageSize: tableData.value.pageSize,
    type:groupType.value
  };
  Object.assign(params, searchForm.value);
  let result = await proxy.Request({
    url: api.loadDataList,
    params: params,
  });
  if (!result) {
    return;
  }
  Object.assign(tableData.value, result.data);
};



//删除
const delInfo = (data) => {
  proxy.Confirm(`确定要删除【${data.title}】吗？`, async () => {
    delShareInfo({ groupId: data.groupId },
    api.delInfo);
  });
};


const delShareInfo = async (params, url) => {
  let result = await proxy.Request({
    url: url,
    params: params,
  });
  if (!result) {
    return;
  }
  proxy.Message.success("删除成功");
  loadDataList();
};

const userGroupChatRef=ref();
const showGropChat=(data)=>{
    userGroupChatRef.value.show(data.groupId);
}

</script>

<style lang="scss" scoped>
.action-bar {
      margin-bottom: 20px;
      padding: 15px;
      background: #fff;
      border-radius: 8px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    }
</style>
